﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Channels;
using Cards;
using Games;
using Rooms;
using Players;

 namespace BackEnd
{
    public partial class WebSocketHandler
    {
        public void _JoinChannel(int idChannel)
        {
            if (bJoinChannel && bJoinRoom == false)
            {
                if (!Channel.outChannel(idchanneljoin, this.ObjPlayer))
                {
                    // out fail
                }
                else
                {
                    idchanneljoin = -1;
                }
            }
            else if (bJoinChannel && bJoinRoom == true)
            {
                return;
            }
            if (Channel.joinChannel(idChannel, this.ObjPlayer))
            {
                idchanneljoin = idChannel;
                bJoinChannel = true;
                for (int i = 0; i < Channel.LstChannels.Count; i++)
                {
                    if (Channel.LstChannels[i].IChannelID.Equals(idChannel))
                    {
                        //send list room in channel
                        this.Send("Please select Room in list");
                        this.Send("You selected channel id = " + idChannel);
                        for (int j = 1; j <= Channel.LstChannels[i].LstRoom.Count; j++)
                        {
                            try
                            {
                                this.Send("Room name: " + Channel.LstChannels[i].LstRoom[j - 1].StrRoomName + ", Room ID: " + Channel.LstChannels[i].LstRoom[j - 1].IIdRoom);
                            }
                            catch (Exception ex)
                            {

                            }
                        }
                        return;
                    }
                }
            }
            else
            {
                // join channel fail
            }
        }
        public void _OutChannel(int idChannel)
        {
            if (!bJoinChannel)
                return;
            if (Channel.outChannel(idChannel, this.ObjPlayer))
            {
                bJoinChannel = false;
                idroomjoin = -1;
                idchanneljoin = -1;
            }
            else
            {
                // out channel fail
            }
        }
        public void _JoinRoom(int idRoom)
        {
            if (bJoinChannel == true && bJoinRoom == false)
            {
                if (Room.joinRoom(Channel.LstChannels[idchanneljoin - 1].LstRoom[idRoom - 1], this.ObjPlayer))
                {
                    if (Channel.outChannel(idchanneljoin, this.ObjPlayer))
                    {
                        idroomjoin = idRoom;
                        bJoinRoom = true;
                        bJoinChannel = false;
                        this.Send("You joined " + Channel.LstChannels[idchanneljoin - 1].LstRoom[idRoom - 1].StrRoomName);
                        this.Send("Infomation room");
                        List<WebSocketHandler> lstTemp = _GetAllClientInRoom(idchanneljoin - 1,idRoom - 1);
                        for (int i = 0; i < lstTemp.Count; i++)
                        {
                            this.Send("Player name " + lstTemp[i].strUsername + ", ready = " + lstTemp[i].statusReady);
                        }
                    }
                    else
                    {
                        // out channel fail
                    }
                }
                else
                {
                    // join room fail
                }
            }
            else
            {
                //joined c
            }
        }
        public void _OutRoom(int idRoom)
        {
            if (bJoinRoom == true && bJoinChannel == false && idroomjoin != -1)
            {
                //if (Room.outRoom(idRoom, ObjPlayer))
                if (Room.joinRoom(Channel.LstChannels[idchanneljoin - 1].LstRoom[idRoom - 1], this.ObjPlayer))
                {
                    if (Channel.joinChannel(idchanneljoin, this.ObjPlayer))
                    {
                        bJoinRoom = false;
                        bJoinChannel = true;
                        idroomjoin = -1;
                    }
                    else
                    {
                        //out channel fail
                    }
                }
                else
                {
                    //out room fail
                }
            }
            else
            {
                //in other room
            }
        }
        public void _ChangeStatus(int status)
        {
            statusReady = status;
        }
        public void _CheckStatusInRoom(int idchannel, int idroom)
        {
            List<WebSocketHandler> lstClient = _GetAllClientInRoom(idchannel, idroom);
            int count1 = lstClient.Count;
            int count2 = 0;
            for (int i = 0; i < count1; i++)
            {
                if (lstClient[i].statusReady == 2)
                {
                    count2++;
                }

            }
            if (count1 == count2)
            {
                // all ready
            }
        }
        public List<WebSocketHandler> _GetAllClientInRoom(int idchannel, int idroom)
        {
            List<WebSocketHandler> lstClient = new List<WebSocketHandler>();
            for (int i = 0; i < Channel.LstChannels[idchannel].LstRoom[idroom].LstPlayers.Count; i++)
            {
                Player _Objplay = Channel.LstChannels[idchannel].LstRoom[idroom].LstPlayers[i];
                for (int j = 0; j < ArrayClient.Count; j++)
                {
                    if (ArrayClient[j].ObjPlayer == _Objplay)
                    {
                        //add client to list
                        lstClient.Add(ArrayClient[j]);
                        break;
                    }
                }
            }
            return lstClient;
        }
        public void _SendMessageToList(List<WebSocketHandler> listClient, string strMessage)
        {
            for (int i = 0; i < listClient.Count; i++)
            {
                listClient[i].Send(strMessage);
            }
        }
    }
   
     
}